<template>
	<el-dialog v-model="dialog" :title="!state.form.id ? '新增' : '修改'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="state.form" :rules="state.rules" label-width="120px" @keyup.enter="submitForm(formRef)" status-icon>
	    				<el-form-item label="主键" prop="id">
					<el-input v-model="state.form.id" placeholder="主键"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="name">
					<el-input v-model="state.form.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="mobile">
					<el-input v-model="state.form.mobile" placeholder="手机号"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="state.form.password" placeholder="密码"></el-input>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="state.form.remark" placeholder="备注"></el-input>
				</el-form-item>
				<el-form-item label="状态-正常/卸任/删除" prop="status">
					<el-input v-model="state.form.status" placeholder="状态-正常/卸任/删除"></el-input>
				</el-form-item>
				<el-form-item label="身份证号码" prop="idCard">
					<el-input v-model="state.form.idCard" placeholder="身份证号码"></el-input>
				</el-form-item>
				<el-form-item label="省编号" prop="provinceCode">
					<el-input v-model="state.form.provinceCode" placeholder="省编号"></el-input>
				</el-form-item>
				<el-form-item label="省名称" prop="provinceName">
					<el-input v-model="state.form.provinceName" placeholder="省名称"></el-input>
				</el-form-item>
				<el-form-item label="市编号" prop="cityCode">
					<el-input v-model="state.form.cityCode" placeholder="市编号"></el-input>
				</el-form-item>
				<el-form-item label="市名称" prop="cityName">
					<el-input v-model="state.form.cityName" placeholder="市名称"></el-input>
				</el-form-item>
				<el-form-item label="区编号" prop="areaCode">
					<el-input v-model="state.form.areaCode" placeholder="区编号"></el-input>
				</el-form-item>
				<el-form-item label="区名称" prop="areaName">
					<el-input v-model="state.form.areaName" placeholder="区名称"></el-input>
				</el-form-item>
				<el-form-item label="街道编号" prop="streetCode">
					<el-input v-model="state.form.streetCode" placeholder="街道编号"></el-input>
				</el-form-item>
				<el-form-item label="街道名称" prop="streetName">
					<el-input v-model="state.form.streetName" placeholder="街道名称"></el-input>
				</el-form-item>
				<el-form-item label="详细地址" prop="address">
					<el-input v-model="state.form.address" placeholder="详细地址"></el-input>
				</el-form-item>
				<el-form-item label="头像" prop="avatar">
					<el-input v-model="state.form.avatar" placeholder="头像"></el-input>
				</el-form-item>
				<el-form-item label="工作状态" prop="workStatus">
					<el-input v-model="state.form.workStatus" placeholder="工作状态"></el-input>
				</el-form-item>
				<el-form-item label="创建时间" prop="createTime">
					<el-input v-model="state.form.createTime" placeholder="创建时间"></el-input>
				</el-form-item>
				<el-form-item label="更新时间" prop="updateTime">
					<el-input v-model="state.form.updateTime" placeholder="更新时间"></el-input>
				</el-form-item>
				<el-form-item label="逻辑删除：0.显示；1.隐藏；" prop="delFlag">
					<el-input v-model="state.form.delFlag" placeholder="逻辑删除：0.显示；1.隐藏；"></el-input>
				</el-form-item>
				<el-form-item label="创建人" prop="createBy">
					<el-input v-model="state.form.createBy" placeholder="创建人"></el-input>
				</el-form-item>
				<el-form-item label="修改人" prop="updateBy">
					<el-input v-model="state.form.updateBy" placeholder="修改人"></el-input>
				</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage,FormInstance } from 'element-plus'
import { addObj, editObj, getById } from '/@/api/cloud/rider'
const emit = defineEmits(['initPage']);
const state = reactive({
	form: {
		id: '',
		name: '',
		mobile: '',
		password: '',
		remark: '',
		status: '',
		idCard: '',
		provinceCode: '',
		provinceName: '',
		cityCode: '',
		cityName: '',
		areaCode: '',
		areaName: '',
		streetCode: '',
		streetName: '',
		address: '',
		avatar: '',
		workStatus: '',
		createTime: '',
		updateTime: '',
		delFlag: '',
		createBy: '',
		updateBy: ''	},
	rules: {
	},
})


const dialog = ref(false);
const loading = ref(false);
const formRef = ref();

const initForm = (row: any | undefined) => {
	dialog.value = true;
	if (row && row.id) {
		getDetail(row.id);
	}
};

const getDetail = (id: string) => {
	loading.value = true;
	getById(id).then((response: any) => {
		state.form = response.data.data;
		loading.value = false;
	});
};

/**
 * 关闭事件
 */
const handleClose = () => {
	resetForm(formRef.value);
};

/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	state.form.id = '';
	loading.value = false;
	dialog.value = false;
	formEl.resetFields();
};


/**
 * 提交按钮
 */
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid) => {
		if (valid) {
			loading.value = true;
			if (state.form.id) {
				//修改
				edit();
			} else {
				//新增
				add();
			}
		}
	});
};
/**
 * 新增
 */
const add = () => {
	addObj(state.form)
			.then(() => {
				resetForm(formRef.value);
				ElMessage.success('新增成功');

				emit('initPage');
			})
			.catch(() => {
				loading.value = false;
			});
};
/**
 * 修改
 */
const edit = () => {
	editObj(state.form)
			.then(() => {
				resetForm(formRef.value);
				ElMessage.success('修改成功');
				emit('initPage');
			})
			.catch(() => {
				loading.value = false;
			});
};

defineExpose({
	initForm
})
</script>
